#include "stdafx.h"

XCONTENTDEVICEID selectedDevice = 0;

void StorageInit() {
	// get storage device
	XOVERLAPPED xol = {0};
	ULARGE_INTEGER bytes = {0};

	xol.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	while(selectedDevice == 0) {

		while(XShowDeviceSelectorUI(0, XCONTENTTYPE_SAVEDGAME, XCONTENT_NONE, bytes, &selectedDevice, &xol) != ERROR_IO_PENDING)
			Sleep(0);
		
		WaitForSingleObject(xol.hEvent, INFINITE);
	}

	CloseHandle(xol.hEvent);
}

HRESULT OpenContent(char* Drive, char* FileName, LPCWSTR DisplayName, DWORD UserIndex) {
	if(selectedDevice == 0)
		return S_FALSE;

	XCONTENT_DATA contentData = {0};
	lstrcpyW(contentData.szDisplayName, DisplayName);
	contentData.dwContentType = XCONTENTTYPE_SAVEDGAME;
	contentData.DeviceID = selectedDevice;
	lstrcpy(contentData.szFileName, FileName);

	DWORD result = XContentCreate(UserIndex, Drive, &contentData, XCONTENTFLAG_OPENALWAYS, NULL, NULL, NULL);
	if(result != ERROR_SUCCESS)
		return S_FALSE;

	return S_OK;
}

HRESULT CloseContent(char* Drive) {
	XContentClose(Drive, NULL);

	return S_OK;
}